package functionalTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.platform.suite.api.Suite;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;


public class ResultTest {
    private static WebDriver webDriver;
    private static String path = "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe";

    @Before
    public void setUp() {
        // 设置 chromeDriver 路径
        System.setProperty("webdriver.chrome.driver", path);
        webDriver = new ChromeDriver();
        // 打开Java API文档搜索项目
        webDriver.get("http://47.120.16.255:8080/index.html");

    }

    @Test
    public void testResults() throws InterruptedException {
        // 检查搜索框和按钮是否正常显示
        WebElement searchBox = webDriver.findElement(By.cssSelector("body > div > div.header > input[type=text]"));
        WebElement searchButton = webDriver.findElement(By.id("search-btn"));
        assertTrue("搜索框应该可见", searchBox.isDisplayed());
        assertTrue("搜索按钮应该可见", searchButton.isDisplayed());

        // 输入查询词并搜索
        String query = "Array";
        searchBox.sendKeys(query);
        searchButton.click();

        // 显式等待搜索结果加载
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div > div.result > div:nth-child(1)")));

        // 展示搜索结果和实际搜索结果应该保持一致
        // 展示搜索结果数
        WebElement resultStats = webDriver.findElement(By.xpath("/html/body/div/div[2]/div[1]"));
        String resultText = resultStats.getText();
        String resultCount = resultText.replaceAll("\\D+", "");
        // 实际展示搜索结果数
        List<WebElement> results = webDriver.findElements(By.className("item"));
        // 断言判断两者是否一致
        assertEquals("显示结果数和实际展示结果数应保持一致", Integer.parseInt(resultCount), results.size());

        int n = 0;
        for(WebElement result : results) {
            // 获取标题属性
            WebElement linkElement = result.findElement(By.tagName("a"));
            String href = linkElement.getAttribute("href"); // 获取 href 属性
            String title = linkElement.getText(); // 获取标题文本

            // 检查标题是否正确显示
            assertNotNull("标题不应为空", title);

            // 检查描述是否包含查询词(包含的查询词已标红)
            WebElement desc = result.findElement(By.tagName("i"));
            assertNotNull("应包含查询词", desc.getText());

            // 检查URL是否正常显示
            WebElement url = result.findElement(By.className("url"));
            assertNotNull("URL不应为空", url.getText());

            // 点击标题是否能够正确跳转
            String expectedUrl = url.getText(); // 获取预取的跳转URL
            linkElement.click(); // 点击标题

            // 获取所有标签页的句柄
            ArrayList<String> tabs = new ArrayList<>(webDriver.getWindowHandles());

            // 切换到新标签页(新标签页一般在索引1的位置)
            webDriver.switchTo().window(tabs.get(1));

            // 等待新页面加载
            WebDriverWait waitUrl = new WebDriverWait(webDriver, Duration.ofSeconds(10));
            waitUrl.until(ExpectedConditions.urlToBe(expectedUrl));

            // 获取当前页面url
            // System.out.println(webDriver.getCurrentUrl());

            // 断言跳转页面URL和期望URL是否一致
            assertEquals("应该跳转到正确的URL", expectedUrl, webDriver.getCurrentUrl());

            // 关闭新标签页
            webDriver.close();

            // 切换回原标签页
            webDriver.switchTo().window(tabs.get(0));

            n++;
            if(n == 10) {
                break;
            }
        }
    }

    @After
    public void quit() {
        webDriver.quit();
    }
}
